<template>
  <XtxDialog :title="formData.id ? '修改收货地址' : '添加收货地址'" v-model:visible="dialogVisible">
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input class="input" placeholder="请输入收货人" v-model="formData.receiver" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input class="input" placeholder="请输入手机号" v-model="formData.contact" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity
              placeholder="请选择所在地区"
              :fullLocation="formData.fullLocation"
              @change="changeCity"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input class="input" placeholder="请输入详细地址" v-model="formData.address" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input class="input" placeholder="请输入邮政编码" v-model="formData.postalCode" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input
              class="input"
              placeholder="请输入地址标签，逗号分隔"
              v-model="formData.addressTags"
            />
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right: 20px" @click="dialogVisible = false"
        >取消</XtxButton
      >
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { ref, reactive } from 'vue'
import { addAddress, updateAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup(props, { emit }) {
    const dialogVisible = ref(false)
    // 打开函数
    const open = address => {
      dialogVisible.value = true
      // 如果address里面有值就是修改，否则就是添加
      if (address.id) {
        // 修改
        for (let k in address) {
          formData[k] = address[k]
        }
      } else {
        // 添加
        // 清空表单
        for (let k in formData) {
          if (k === 'isDefault') {
            formData[k] = 1
          } else {
            formData[k] = null
          }
        }
      }
    }
    // 表单数据
    // receiver	string	必须	测试	收货人姓名
    // contact	string	必须		联系方式
    // provinceCode	string	必须		所在省份编码
    // cityCode	string	必须		所在城市编码
    // countyCode	string	必须		所在区/县编码
    // address	string	必须		详细地址
    // postalCode	string	必须		邮政编码
    // addressTags	string	必须		地址标签,以英文逗号分割
    // isDefault	integer	必须		是否为默认，0为是，1为否
    const formData = reactive({
      receiver: null,
      contact: null,
      provinceCode: null,
      cityCode: null,
      countyCode: null,
      address: null,
      postalCode: null,
      addressTags: null,
      isDefault: 1,
      // 城市组件选择显示
      fullLocation: null
    })
    // 城市地区选择
    const changeCity = result => {
      const { provinceCode, cityCode, countyCode, fullLocation } = result
      formData.provinceCode = provinceCode
      formData.cityCode = cityCode
      formData.countyCode = countyCode
      formData.fullLocation = fullLocation
    }
    // 确认
    const submit = () => {
      if (formData.id) {
        // 修改
        updateAddress(formData).then(() => {
          Message({ type: 'success', text: '修改收货地址成功' })
          dialogVisible.value = false
          emit('onSuccess', formData)
        })
      } else {
        addAddress(formData).then(data => {
          formData.id = data.result.id
          Message({ type: 'success', text: '添加收货地址成功' })
          dialogVisible.value = false
          emit('onSuccess', formData)
        })
      }
    }
    return { dialogVisible, open, formData, changeCity, submit }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  .xtx-form-item {
    display: flex;
    align-items: center;
    width: 700px;
    margin: 0 auto;
    padding-bottom: 25px;
    .label {
      width: 180px;
      padding-right: 10px;
      text-align: right;
      color: #999;
    }
    .field {
      width: 320px;
      height: 50px;
      position: relative;
    }
    input {
      outline: none;
      border: 1px solid #e4e4e4;
      width: 320px;
      height: 50px;
      line-height: 50px;
      padding: 0 10px;
      &::placeholder {
        color: #ccc;
      }
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
